/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.relations.index.indexTask;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2010-12-15
 * 
 */
public class IndexTaskForEachThread {

	/**
	 * @param product
	 *            tasks to be processed
	 * @param logFile
	 * @throws SQLException
	 * @throws IOException
	 */
	public IndexTaskForEachThread(ArrayList<Integer> task,
			ArrayList<DatasetItem> dss) throws SQLException, IOException {
		System.out.println("Start doing. From: " + task.get(0));
		// TODO Auto-generated constructor stub
		Connection con = DBConnectionFactory.getConnection();//.getFalconConnection();
		PreparedStatement ps = con
				.prepareStatement("SELECT uri FROM uri where id = ?");
		ArrayList<DatasetItem> tmp = new ArrayList<DatasetItem>();
		for (int id : task) {
			ps.setInt(1, id);
			ResultSet rs1 = ps.executeQuery();
			if (rs1.next()) {
				String uri = rs1.getString(1);
				DatasetItem ds = new DatasetItem("uri", id, uri);
				tmp.add(ds);
			}
			rs1.close();
		}
		ps.close();
		con.close();

		synchronized (IndexTask.lock) {
			for (DatasetItem dsi : tmp) {
				dss.add(dsi);
			}
		}
		tmp.clear();
		System.out.println("Done. From: " + task.get(0));
	}

}
